package com.ld.admin.controller.system;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SysRoleModel;
import com.ld.admin.service.system.SysMenuService;
import com.ld.admin.service.system.SysRoleService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 角色
 * 
 * @ClassName SysRoleController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年03月27日 14:35:40
 *
 */
@Api(tags = "角色")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "role.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemSysRoleController")
@RequestMapping("${shieldsb.adminPath}/system/role")
public class SysRoleController extends AdminBasicController<SysRoleModel> {
    @Autowired
    protected SysRoleService sysRoleService;
    @Autowired
    protected SysMenuService menuPermService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/system/role/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/system/role/sysrole_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = sysRoleService.getSearchQueryModel(request, getModelClass());
        sysRoleService.getPageBean(request, getModelClass(), queryModel);
        return "a/system/role/sysrole_list";
    }

    @ApiOperation(value = "角色展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        sysRoleService.getOne(request, getModelClass(), id);
        return "a/system/role/sysrole_show";
    }

    @ApiOperation(value = "跳转到添加/修改角色", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        sysRoleService.getOne(request, getModelClass(), id);
        return "a/system/role/sysrole_update";
    }

    @ApiOperation(value = "添加/修改角色", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid SysRoleModel model) {
        Result result = sysRoleService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除角色", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = sysRoleService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "获取权限结果", notes = "")
    @RequestMapping(path = "/permissiondata", method = RequestMethod.GET)
    // 返回json格式
    @ResponseBody
    public Object permissiondata(String... args) throws ServletException, IOException {
        long roleId = getLong("id");
        Result result = menuPermService.getRolePermData(request, roleId);
        return Web.Response.resultWithData(result);
    }

    @ApiOperation(value = "保存角色权限", notes = "")
    @RequestMapping(path = "/savePermission", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object savePermission(String... args) throws ServletException, IOException {
        long roleId = getLong("id");
        Result result = menuPermService.saveRolePerms(roleId);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "跳转拥有该角色的成员", notes = "")
    @RequestMapping(path = "/toRoleUser", method = RequestMethod.GET)
    public String toRoleUser() {
        setAttributes();
        return "a/system/role/sysrole_user_search";
    }

    @ApiOperation(value = "跳转拥有该角色的成员", notes = "")
    @RequestMapping(path = "/sysrole_user_search", method = RequestMethod.POST)
    public String sysrole_user_search() {
        Long roleId = getLong("roleId");
        int pageSize = 10; // 每页显示10条
        sysRoleService.getRoleUserListBean(request, roleId, pageSize);
        return "a/system/role/sysrole_user_list";
    }

}